<?php namespace Hdphp\Cli;

//命令行模式
class Cli{
	/**
	 * 运行
	 * @return [type] [description]
	 */
	public static function run()
	{
		$instance = new self;
		if(isset($_SERVER['argv'][1]))
		{
			$info = explode(':',$_SERVER['argv'][1]);
			$instance->$info[0]();
		}
	}

	/**
	 * 创建模型或控制器
	 * @return [type] [description]
	 */
	public function make()
	{
		$info = explode(':',$_SERVER['argv'][1]);
		$method='make'.ucfirst($info[1]);
		$this->$method($_SERVER['argv'][2]);
	}

	/**
	 * 创建模型
	 * @param  [type] $model [description]
	 * @return [type]        [description]
	 */
	public function makeModel($model)
	{	
		$info =explode('.',$model);
		$MODULE = $info[0];
		$MODEL = $info[1];
		$TABLE = strtolower($MODEL);
		$file = APP_PATH.'/'.$MODULE.'/Model/'.ucfirst($MODEL).'.php';

		//判断目录
		if(!is_dir(APP_PATH.'/'.$MODULE.'/Model')) 
		{
			die("Directory does not exist\n");
		}

		//创建模型文件
		if(is_file($file))
		{
			die("Model file already exists\n");
		}
		else
		{
			$namespace = $MODULE.'\\Model';

			$data = file_get_contents(__DIR__.'/Demo/Model.php');
			$data = str_replace(array('{{MODULE}}','{{MODEL}}','{{TABLE}}'), 
				array($MODULE,$MODEL,$TABLE), $data);
			file_put_contents($file, $data);
		}
	}
}


